package org.zhigong.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.zhigong.dto.SysResult;
import org.zhigong.dto.SysSearch;
import org.zhigong.entity.Zhiwei;
import org.zhigong.service.ZhiweiService;

import java.util.List;

import static org.zhigong.util.CommonUtil.isEmpty;
import static org.zhigong.util.CommonUtil.isNumeric;

@Controller
public class ZhiweiController {

    @Autowired
    private ZhiweiService zhiweiService;

    // 只做跳转用,初始化使用json获取数据处理.
    @RequestMapping(value = "/zhiweis", method = RequestMethod.GET)
    public String init(){
        return "zhiwei";
    }

    // 分页查询获取列表,返回json
    @RequestMapping(
            value = "/zhiweis",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<List<Zhiwei>> search(SysSearch sysSearch){

        SysResult<List<Zhiwei>> sysResult;
        if(sysSearch.getPageNumber() < 0){
            try{
                List<Zhiwei> zhiweiList = zhiweiService.getZhiweiListAll();
                sysResult = new SysResult<List<Zhiwei>>(true, zhiweiList);
            }catch (Exception e){
                sysResult = new SysResult<List<Zhiwei>>(false, e.getMessage());
            }
        }else{
            Zhiwei zhiwei = new Zhiwei();
            String condS = sysSearch.getCondition();
            zhiwei.setMc(condS);
            zhiwei.getPage().setCurrentPage(sysSearch.getCurrentPage());
            zhiwei.getPage().setPageNumber(sysSearch.getPageNumber());
            try{
                List<Zhiwei> zhiweiList = zhiweiService.getZhiweiList(zhiwei);
                if(zhiweiList.size() > 0){
                    zhiweiList.get(0).setPage(zhiwei.getPage());
                }
                sysResult = new SysResult<List<Zhiwei>>(true, zhiweiList);
            }catch (Exception e){
                sysResult = new SysResult<List<Zhiwei>>(false, e.getMessage());
            }
        }


        return sysResult;
    }

    // 新增
    @RequestMapping(
            value = "/zhiweis",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> create(@RequestBody Zhiwei zhiwei){
        SysResult<Integer> sysResult;
        try{
            Boolean add  = zhiweiService.addZhiwei(zhiwei);
            sysResult = new SysResult<Integer>(true, add ? 1 : 0);
        }catch (Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }


    // 删除
    @RequestMapping(
            value = "/zhiweis/{zwbh}",
            method = RequestMethod.DELETE,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> delete(@PathVariable("zwbh") int zwbh){
        SysResult<Integer> sysResult;
        try{
            Boolean del = zhiweiService.deleteZhiwei(zwbh);
            sysResult = new SysResult<Integer>(true, del ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 更新
    @RequestMapping(
            value = "/zhiweis/{zwbh}",
            method = RequestMethod.PUT,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Integer> update(@RequestBody Zhiwei zhiwei){
        SysResult<Integer> sysResult;
        try{
            Boolean upd = zhiweiService.updateZhiwei(zhiwei);
            sysResult = new SysResult<Integer>(true, upd ? 1 : 0);
        }catch(Exception e){
            sysResult = new SysResult<Integer>(false, e.getMessage());
        }
        return sysResult;
    }

    // 查看
    @RequestMapping(
            value = "/zhiweis/{zwbh}",
            method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SysResult<Zhiwei> view(@PathVariable("zwbh") int zwbh){
        SysResult<Zhiwei> sysResult;
        try{
            Zhiwei zhiwei = zhiweiService.getZhiweiByZwbh(zwbh);
            sysResult = new SysResult<Zhiwei>(true, zhiwei);
        }catch(Exception e){
            sysResult = new SysResult<Zhiwei>(false, e.getMessage());
        }
        return sysResult;
    }
}
